<?php
namespace Home\Controller;
use Think\Controller;
header('content-Type=text/html;charset=utf-8');
class CommentController extends Controller {
    public function index(){
        $c=D('Comment');
        $num=$c->field('id')->count();
        $this->assign('num',$num);
        //调用评论列表，并输出
        $result= $this->getlist();
       
        $data=array();
        foreach($result as $key=>$value){
            if(is_array($value)) {
                foreach($value as $v){
                   $data[]=$v;
                }

            }else echo'出现错误';
        }
       $this->assign('data',$data);
        $this->display();
    }
    //使用无限极分类获取评论列表
    public function getlist($pid=0,&$result=array(),$blank=0){
        $c=D('Comment');
        $data['pid']=$pid;
        $res=$c->where($data)->select();
          $blank=$blank+2;
        if(!$res){
            return array();
        }else{
                foreach($res as $key=>$value){
                    $value['username']=str_repeat("&nbsp",$blank).$value['username'];
                    $value['content']=str_repeat("&nbsp",$blank).$value['content'];
                   $result[]=array($key=>$value);
                 $this->getlist($value['id'],$result,$blank);
               }
      }
        return $result;
    }
    public function display_list(){
        $a =array(  '0'=>array(
          'name'=>'zhaowneyi',
          'id'=>'i'),
            '1'=>array(
          'name'=>'lixoa',
          'id'=>'i')
          );
        foreach($a as $key=>$value){
          echo $value['name'];
        }
    }
    public function comment(){
     
       
     //处理回复
        $pid=$_GET[id];
        echo $pid;
        $content=$_POST['text'];
        echo $content;
      //处理评论  
    if($_POST['submit']=="提交评论") {
            $pid=0; 
            $content=$_POST['comment'];
        }
      
     $c=D('Comment');
     $data['id']=$c->max('id')+1;
     $data['pid']=$pid;
     $data['content']=$content;
     $data['create_time']= date("Y-m-d h:i:s",time()) ;
     $res=$c->add($data);
     if($res>0){
        $this->success('发表评论成功','index',10);
     }else{
        $this->error('发表评论失败','index',3);
     }


    }
   
   
    
}